home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 03 - 1987 / 03.01 Jan 87 / c source / printw.c < prev   
Encoding:
C/C++ Source or Header  |  1986-12-05  |  8.5 KB  |  304 lines  |  [TEXT/KAHL]

  1.  #include    "abc.h"
  2.  #include    "Quickdraw.h"
  3.  #include    "EventMgr.h"
  4.  #include    "WindowMgr.h"
  5.  #include    "MenuMgr.h"
  6.  #include    "FontMgr.h"
  7.  
  8.  /* printw()
  9.  *
  10.  * Displays strings and numbers in a 
  11.  * special window
  12.  *
  13.  *    This function is designed to receive
  14.  *    a variable number of parameters. The 
  15.  *    number is computed by the number of
  16.  *    percent signs in the control string.
  17.  *    If the number of parameters following the 
  18.  *    control string does not match the 
  19.  *    number of percent signs, expect 
  20.  *    the unexpected.
  21.  */
  22. printw(cs)
  23.     char    *cs;        /* the control string */
  24. {
  25. #define    Bufsz    14        /* size of buffer to hold */
  26.                         /* converted numbers */    
  27.  
  28.     static Rect                boundsRect;    /* variables for */
  29.     static Rect                windowRect;    /* defining printw */
  30.     static WindowRecord        wrc;        /* window, pw is */
  31.     static WindowPtr        pw = 0;        /* initialized to 0 */
  32.     static short            linesz;        /* size of line */
  33.     
  34.     WindowPtr                oldport;        /* save grafport here */
  35.     FontInfo                    info;            
  36.     short                        nl;
  37.     Point                        pt;
  38.     RgnHandle                updrgn;        /* needed for scrolling */
  39.     char                        numAsStr[Bufsz];    /* number conversion */
  40.     short                        nsz;            /* size of numbers (2 or 4) */
  41.     char                        **ts;            /* ptr to ptr to ctrl string */
  42.     char                        *ps;            /* ptr to parameters */
  43.     ulong                        num;            /* for number conversion */
  44.     short                        convchar;    /* found conversion char */
  45.     short                        islong;        /* number is a long */
  46.     short                        ishex;        /* display in hex digits */
  47.     short                        isp;            /* pascal string or point */
  48.     short                        isr;            /* rectangle */
  49.     Point                        *ptp;
  50.     Point                        ***ytp;
  51.     Point                        *xtp;
  52.     Rect                        *rtp;
  53.     char                        c;                /* char parameter */
  54.     char                        *s;            /* string pointer parameter */
  55.     long                        tcs;
  56.     char                        *tps;
  57.     
  58. /* Window rectancgle coordinates */
  59.  
  60. #define    wl        0
  61. #define    wr        512
  62. #define    wt        250
  63. #define    wb        342
  64.     
  65.     
  66.     GetPort(&oldport);                    /* save current graph port */
  67.     if (pw equals 0)                        /* if window does not exist, */
  68.         {                                        /*  open it */
  69.         SetRect(&boundsRect,wl,wt,wr,wb);
  70.         pw = NewWindow(&wrc, &boundsRect,
  71.                     CtoPstr(""),True,plainDBox,
  72.                     (WindowPtr) -1, True, 0);
  73.         GetFontInfo(&info);                    /* compute line height */
  74.         linesz = info.ascent + info.descent;
  75.         nl = linesz;                        /* move down one line as */
  76.         }                                        /*  writing will be above */
  77.     else                                        /*  boundary.  No need to  */
  78.         nl = 0;                                /*  move line if already open */
  79.     SetPort(pw);                            /* Set graf port to this window */
  80.     Move(0,nl);                                /* Move (relative) */
  81.     
  82.     ts = &cs;                                /* get address of control string ptr */
  83.     ps = (char *)ts;                        /* convert to pointer to params */
  84.     ps += sizeof(long);                    /* skip over control string ptr */
  85.     tcs = (long)cs;
  86.     tps = ps;
  87.     while (*cs)                                /* loop until end of control string */
  88.         {
  89.         switch (*cs)                        /* check each character */
  90.             {
  91.             case '%' :                        /* percent sign: check conversion */
  92.                 cs++;                            /* point to next char */
  93.                 convchar = False;            /* initialize for conversion loop */
  94.                 islong = False;
  95.                 ishex  = False;
  96.                 isp      = False;
  97.                 isr     = False;
  98.                 do {                            /* loop until reach conversion char */
  99.                     switch (*cs)
  100.                         {
  101.                         case 'l' :            /* indicates a long */
  102.                             islong = True;
  103.                             cs++;
  104.                             break;
  105.                         case 'r' :
  106.                             isr = True;
  107.                             cs++;
  108.                             break;
  109.                         case 'p' :
  110.                             isp = True;
  111.                             cs++;
  112.                             break;
  113.                         case 'u'    :            /* unsigned decimal, conversion char */
  114.                         case 'd' :            /* signed decimal, conversion char */
  115.                             if (islong)        /* extract number from param */
  116.                                 {
  117.                                 num = *(ulong*)ps;
  118.                                 nsz = sizeof(long);
  119.                                 }
  120.                             else
  121.                                 {
  122.                                 num = *(ushort*)ps;
  123.                                 nsz = sizeof(short);
  124.                                 }
  125.                             ps += nsz;        /* point to next param */
  126.                             ntoa(num,nsz,'u' - *cs,numAsStr);/* convert number */
  127.                             DrawString(CtoPstr(numAsStr));    /* write number */
  128.                             convchar = True;
  129.                             break;
  130.                         case 'x' :
  131.                             if (islong)        /* extract number from param */
  132.                                 {
  133.                                 num = *(ulong*)ps;
  134.                                 nsz = sizeof(long);
  135.                                 }
  136.                             else
  137.                                 {
  138.                                 num = *(ushort*)ps;
  139.                                 nsz = sizeof(short);
  140.                                 }
  141.                             ntox(num,nsz,numAsStr);
  142.                             DrawString(CtoPstr(numAsStr));
  143.                             convchar = True;
  144.                             ps += nsz;        /* point to next param */
  145.                             break;
  146.                         case 's'    :
  147.                             s = *(char **)ps;
  148.                             if (isp)
  149.                                 DrawString(s);
  150.                             else
  151.                                 {
  152.                                 DrawString(CtoPstr(s));
  153.                                 PtoCstr(s);
  154.                                 }
  155.                             ps += sizeof(char*);        
  156.                             convchar = True;
  157.                             break;    
  158.                         case 't' :
  159.                             if (isp)
  160.                                 {
  161.                                 ptp = *(Point **)ps;/*!!!*/
  162.                                 DrawString ("\pPoint h: ");
  163.                                 ntoa((ulong)ptp->h,2,True,numAsStr);
  164.                                 DrawString(CtoPstr(numAsStr));
  165.                                 DrawString ("\p  v: ");
  166.                                 ntoa((ulong)ptp->v,2,True,numAsStr);
  167.                                 DrawString(CtoPstr(numAsStr));
  168.                                 }
  169.                             else if (isr)
  170.                                 {
  171.                                 rtp = *(Rect**)ps;
  172.                                 DrawString ("\pRect t: ");
  173.                                 ntoa ((ulong)rtp->top,2,True,numAsStr);
  174.                                 DrawString(CtoPstr(numAsStr));
  175.                                 DrawString("\p l: ");
  176.                                 ntoa ((ulong)rtp->left,2,True,numAsStr);
  177.                                 DrawString(CtoPstr(numAsStr));
  178.                                 DrawString("\p b: ");
  179.                                 ntoa ((ulong)rtp->bottom,2,True,numAsStr);
  180.                                 DrawString(CtoPstr(numAsStr));
  181.                                 DrawString("\p r: ");
  182.                                 ntoa ((ulong)rtp->right,2,True,numAsStr);
  183.                                 DrawString(CtoPstr(numAsStr));
  184.                                 }
  185.                             ps += sizeof(char*);
  186.                             convchar = True;
  187.                             break;
  188.                         
  189.                         case 'c'    :
  190.                             c = *(ushort*)ps;
  191.                             DrawChar(c);
  192.                             nsz = sizeof(short);
  193.                             convchar = True;
  194.                             ps += nsz;
  195.                             break;
  196.                         default    :            /* if it is not any expected char, */
  197.                             DrawChar(*cs);    /*  write the char out and go on */
  198.                             convchar = True;
  199.                         } 
  200.                     } while (not convchar);
  201.                 break;
  202.             case '\n' :                        /* newline ('\n') in control string */
  203.                 GetPen(&pt);                /* find current pen position */
  204.                 if (pt.v+linesz > wb-wt)/* if it goes off window, */
  205.                     {                            /* scroll the window */
  206.                     updrgn = NewRgn();
  207.                     ScrollRect(&(pw->portRect),0,-linesz,updrgn);
  208.                     DisposeRgn(updrgn);    /* nothing to update */
  209.                     Move(0,-linesz);        /* move back onto window */
  210.                     }
  211.                 Move(-pt.h,linesz);        /* move to begining of next line */        
  212.                 break;
  213.             default :                        /* any other character just gets */
  214.                 DrawChar(*cs);                /*  written on the window */
  215.             }
  216.         cs++;                                    /* move pointer to next char */
  217.         }                                        /*  in control string and continue */
  218.         
  219.     SetPort(oldport);                        /* restore orignal graf port */
  220. }
  221.  
  222. /*    Convert numbers to ascii strings
  223.  *        Handles signed and unsigned
  224.  *        short and long values
  225.  *    Note:    Length of string returned 
  226.  *            must be large enough to 
  227.  *            hold -2G (12 bytes)
  228.  */
  229. ntoa(n,len,issigned,s)
  230.     ulong            n;            /* number to convert */
  231.     short            len;        /* size of n (2 or 4)*/
  232.     short            issigned;/* signed flag (True = Signed) */
  233.     char            *s;        /* string to return */
  234. {
  235.     char             ts[12];    /* temporary string */
  236.     int             i = 0;    /* counter, initialized */
  237.     ulong            m;            /* working copy of */
  238.     long            sm;        /* to convert signed values */
  239.     
  240.     if (n equals 0)         /* if n is zero, place '0' */
  241.         ts[i++] = '0';        /*  in temporary string */
  242.     else
  243.         {
  244.         if (issigned)        /* if sign flag is set, */
  245.             {                    /*  convert to signed value */
  246.             if (len equals sizeof(long))
  247.                 sm = (long)n;
  248.             else
  249.                 sm = (short)n;
  250.             if (issigned = sm < 0)    /* Check if value is */
  251.                 n = -sm;                    /*  negative. If so, */
  252.             }                                /*  keep the flag and */
  253.                                             /*  get the absolute value */
  254.         while (n)                        /* Convert number into ascii */
  255.             {                                /*  by repeatedly taking mod */
  256.             ts[i++] = n % 10 + '0';    /*  and dividing.  This */
  257.             n /= 10;                        /*  gives a string in */
  258.             }                                /*  reverse order */
  259.         if (issigned)                    /* If number was negative, */
  260.             ts[i++] = '-';                /*  stick a minus sign in */
  261.         }                                    /*  the string.*/
  262.                 
  263.     do    {                                    /* Reverse the string */
  264.         *s++ = ts[--i];                /*  to the correct direction*/
  265.         }    
  266.     while (i);
  267.  
  268.     *s = '\0';                            /* Place null terminator on */
  269. }                                            /*  string */
  270.  
  271. /*    Convert numbers to hex strings
  272.  *        Handles unsigned
  273.  *        short and long values
  274.  *    Note:    Length of string returned 
  275.  *            must be large enough to 
  276.  *            hold 4G (12 bytes)
  277.  */
  278. ntox(n,len,s)
  279.     ulong            n;            /* number to convert */
  280.     short            len;        /* size of n (2 or 4)*/
  281.     char            *s;        /* string to return */
  282. {
  283.     int             i = 0;    /* counter, initialized */
  284.     short            nibble;    /* one nibble */
  285.     
  286.     static char    hexcode[] = {'0','1','2','3','4','5','6','7',
  287.                                      '8','9','A','B','C','D','E','F'};
  288.  
  289.     
  290.     if (n equals 0)         /* if n is zero, place '0' */
  291.         s[i++] = '0';        /*  in string */
  292.     else
  293.         {
  294.         i = len *= 2;                                    
  295.         while (len)                        
  296.             {
  297.             nibble = n & 0x0000000F;        
  298.             s[--len] = hexcode[nibble]; 
  299.             n >>= 4;
  300.             }                                
  301.         }                                    
  302.     s[i] = 0;        
  303. }
  304.